package djboom.materials.display.test {
	import djboom.materials.state.Font;

	import org.casalib.display.CasaSprite;
	import org.casalib.display.CasaTextField;
	import org.casalib.util.DrawUtil;

	import flash.display.BlendMode;
	import flash.display.GradientType;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 08.04.2010
	 * 
	 */

	public class QuestionButton extends CasaSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		private static const TITLE_TFORMAT:TextFormat = new TextFormat(Font.MYRIAD_PRO_COND, 16, 0x444444, true);

		private static const DROPSHADOW_FILTER:DropShadowFilter = new DropShadowFilter(1, 90, 0xffffff, 1, 0, 0, 1, 1, false, true);

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _enabled:Boolean;
		private var _titleFieldShadow:CasaTextField;
		private var _titleField:CasaTextField;

		private var _gradientMatrix:Matrix = new Matrix();

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function QuestionButton(childIndex:int) {
			super();
			
			_gradientMatrix.createGradientBox(79, 28, -Math.PI / 2);
            
			graphics.lineStyle(2, 0x919191);
			graphics.beginGradientFill(GradientType.LINEAR, [0xa0a0a0, 0xe0e0e0], [1, 1], [0, 255], _gradientMatrix);
			DrawUtil.drawShape(graphics, new Array(new Point(8, 0), new Point(71, 0), new Point(79, 14), new Point(71, 28), new Point(8, 28), new Point(0, 14)));
			graphics.endFill();
			
			_titleFieldShadow = new CasaTextField();
			_titleFieldShadow.embedFonts = true;
			_titleFieldShadow.selectable = false;
			_titleFieldShadow.defaultTextFormat = TITLE_TFORMAT;
			_titleFieldShadow.autoSize = TextFieldAutoSize.LEFT;
			_titleFieldShadow.antiAliasType = AntiAliasType.ADVANCED;
			_titleFieldShadow.text = "Вопрос " + String(childIndex + 1);
			_titleFieldShadow.mouseEnabled = false;
			_titleFieldShadow.thickness = 30;
			_titleFieldShadow.blendMode = BlendMode.OVERLAY;
			_titleFieldShadow.filters = [DROPSHADOW_FILTER];          
			_titleFieldShadow.alpha = 0.5;
			_titleFieldShadow.x = this.width * 0.5 - _titleFieldShadow.textWidth * 0.5 - 3;			_titleFieldShadow.y = 2;
            
			_titleField = new CasaTextField();
			_titleField.embedFonts = true;
			_titleField.selectable = false;
			_titleField.defaultTextFormat = TITLE_TFORMAT;
			_titleField.autoSize = TextFieldAutoSize.LEFT;
			_titleField.antiAliasType = AntiAliasType.ADVANCED;
			_titleField.text = "Вопрос " + String(childIndex + 1);
			_titleField.mouseEnabled = false;
			_titleField.alpha = 0.85;
			_titleField.thickness = 30;
			_titleField.x = _titleFieldShadow.x;
			_titleField.y = _titleFieldShadow.y;
			
			addChild(_titleFieldShadow);
			addChild(_titleField);
			
			addEventListener(Event.REMOVED_FROM_STAGE, clearPage);
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function clearPage(event:Event):void {
			this.graphics.clear();
			
			_titleField.destroy();
			_titleField = null;
            
			_titleFieldShadow.destroy();
			_titleFieldShadow = null;
		}

		public function set enabled(enabled:Boolean):void {
			_enabled = enabled;
			
			this.graphics.clear();
			
			if(_enabled) {
				_titleField.textColor = 0x2e3408;
				
				graphics.lineStyle(2, 0x798f00);
				graphics.beginGradientFill(GradientType.LINEAR, [0x8ca311, 0xc8e42c], [1, 1], [0, 255], _gradientMatrix);
				DrawUtil.drawShape(graphics, new Array(new Point(8, 0), new Point(71, 0), new Point(79, 14), new Point(71, 28), new Point(8, 28), new Point(0, 14)));
				graphics.endFill();
			} else {
				_titleField.textColor = 0x444444; 
				
				graphics.lineStyle(2, 0x919191);
				graphics.beginGradientFill(GradientType.LINEAR, [0xa0a0a0, 0xe0e0e0], [1, 1], [0, 255], _gradientMatrix);
				DrawUtil.drawShape(graphics, new Array(new Point(8, 0), new Point(71, 0), new Point(79, 14), new Point(71, 28), new Point(8, 28), new Point(0, 14)));
				graphics.endFill();
			}
		}
		
        
		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------
		
		// ----------------------------------------------------------------------------
        // Публичные свойства.
        // ----------------------------------------------------------------------------
	}
}
